//
//  ViewController.m
//  NstimerDemo
//
//  Created by wanglei on 17/1/14.
//  Copyright © 2017年 wanglei. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property(nonatomic,strong)dispatch_source_t timer;
@property(nonatomic,strong)NSTimer *paintingTimer;
@property(nonatomic,strong)CADisplayLink *displayLink;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}


- (IBAction)beganClick:(id)sender {
    // [self startPaintingOne];
    // [self startPaintingTwo];
    // [self startPaintingThree];
    //[self startPaintingFour];
     [self startPaintingFive];
    
    
}
- (IBAction)stopClick:(id)sender {
    
    // [self stopPainting];// 停止定时器
    [self destoryTimer];// 销毁计时器
    [self stopDisplayLink];
}


// 停止定时器
- (void) stopPainting{
    if (self.paintingTimer != nil){
        // 定时器调用invalidate后，就会自动执行release方法。不需要在显示的调用release方法
        [self.paintingTimer invalidate];
    }
}



// 定时器执行的方法
- (void)paint:(NSTimer *)paramTimer{
    
    NSLog(@"在线程%@中执行定时器方法",[NSThread currentThread]);
}

/*--------------------------------方法1--(在主线程执行定时器方法)-----------------------------------*/


// 开始定时器
- (void) startPaintingOne{
    /*
     seconds：需要调用的毫秒数
     target：调用方法需要发送的对象。即：发给谁
     userInfo：发送的参数
     repeats：指定定时器是否重复调用目标方法
     */
    // 定义一个NSTimer,此种方法创建的定时器不用加入RunLoop,就可以运行,只要调用就可以运行
    self.paintingTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                                          target:self
                                                        selector:@selector(paint:)  userInfo:nil
                                                         repeats:YES];
    
    // 定义一个NSTimer,此种方法创建的定时必须加入到RunLoop中才可以运行,然后RunLoop再Run才行,详细使用方法见下面介绍
    //    self.paintingTimer = [NSTimer timerWithTimeInterval:1.0
    //                                                 target:self
    //                                               selector:@selector(paint:)
    //                                               userInfo:nil
    //                                                repeats:YES];
    
}




/*--------------------------------方法2---(在子线程执行定时器方法)----------------------------------*/
- (void)startPaintingTwo{
    
    self.paintingTimer = [NSTimer timerWithTimeInterval:1.0
                                                 target:self
                                               selector:@selector(paint:)
                                               userInfo:nil
                                                repeats:YES];
    
    
    
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    
    [queue addOperationWithBlock:^{
        [self startPaintingTwo];
        NSRunLoop *loop = [NSRunLoop currentRunLoop];
        [loop addTimer:self.paintingTimer forMode:NSDefaultRunLoopMode];
        [loop run];
    }];
}





/*--------------------------------方法3-------------------------------------*/


- (void) startPaintingThree{
    // 定义将调用的方法
    SEL selectorToCall = @selector(paint:);
    // 为SEL进行 方法签名
    NSMethodSignature *methodSignature =[[self class] instanceMethodSignatureForSelector:selectorToCall];
    // 初始化NSInvocation
    NSInvocation *invocation =[NSInvocation invocationWithMethodSignature:methodSignature];
    [invocation setTarget:self];
    [invocation setSelector:selectorToCall];
    self.paintingTimer = [NSTimer timerWithTimeInterval:1.0
                                             invocation:invocation
                                                repeats:YES];
    
    //主线程执行方法
    // [[NSRunLoop currentRunLoop] addTimer:self.paintingTimer forMode:NSDefaultRunLoopMode];
    
    //子线程执行方法
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    
    [queue addOperationWithBlock:^{
        NSRunLoop *loop = [NSRunLoop currentRunLoop];
        [loop addTimer:self.paintingTimer forMode:NSDefaultRunLoopMode];
        [loop run];
    }];
}



/*--------------------------------方法4-------------------------------------*/

- (void)startPaintingFour{
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        NSLog(@"这里的线程为%@",[NSThread currentThread]);
        // 这里表示可以切回主线程,也可以就在子线程里面执行代码
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"%@--这里的代码每秒在执行一次么?",[[NSDate alloc]init]);
        });
    });
    // 开启计时器
    dispatch_resume(_timer);
}

// 销毁计时器
-(void)destoryTimer{
    if (_timer) {
        dispatch_source_cancel(_timer);
        _timer = nil;
    }
}


/*--------------------------------方法5-------------------------------------*/

- (void)startPaintingFive{
    
    self.displayLink = [CADisplayLink displayLinkWithTarget:self
                                                   selector:@selector(handleDisplayLink:)];
    [self.displayLink addToRunLoop:[NSRunLoop currentRunLoop]
                           forMode:NSDefaultRunLoopMode];
    
}

- (void)handleDisplayLink:(CADisplayLink *)displayLink{
    
    NSLog(@"代码执行到这里了么");
}

// 注销计时器
- (void)stopDisplayLink{
    [self.displayLink invalidate];
    self.displayLink = nil;
}

@end
